package lanou.giftsay_01.main.categorymain;

import android.graphics.PorterDuffXfermode;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import lanou.giftsay_01.R;
import lanou.giftsay_01.adapter.PopRvAdapter;
import lanou.giftsay_01.adapter.RightAdapter;
import lanou.giftsay_01.adapter.SelectGiftRvAdapter;
import lanou.giftsay_01.bean.FilterBean;
import lanou.giftsay_01.bean.SearchBean;
import lanou.giftsay_01.bean.SelectGiftAllBean;
import lanou.giftsay_01.tools.BaseActivity;
import lanou.giftsay_01.tools.NetTool;
import lanou.giftsay_01.tools.URLValues;



/**
 * Created by dllo on 16/8/23.
 */
public class SelectGiftActivity  extends BaseActivity implements View.OnClickListener, SelectGiftOnClickListenner {
    private ImageView imageView;
    private PopupWindow popupWindow , pxPopupWindow;
    private RecyclerView rv;
    private SelectGiftRvAdapter selectGiftRvAdapter;
    private ImageView pxIv;
    private String url;
    String dx ="";
    String ch ="";
    String gx ="";
    String jg ="";
    private RelativeLayout dxrl;
    private RelativeLayout chrl;
    private RelativeLayout gxrl;
    private RelativeLayout jgrl;
    private ImageView iv;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv4;
    private TextView dxTv;
    private TextView chTv;
    private TextView gxTv;
    private TextView jgTv;
    private NetTool netTool;
    private PopRvAdapter popRvAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_selectgift;
    }
    @Override
    protected void initView() {
        popRvAdapter = new PopRvAdapter(this);
        pxIv = (ImageView) findViewById(R.id.selectgift_paixu_iv);
        pxIv.setOnClickListener(this);
        dxrl = (RelativeLayout) findViewById(R.id.rl_gift);
        chrl = (RelativeLayout) findViewById(R.id.rl_gift2);
        gxrl = (RelativeLayout) findViewById(R.id.rl_gift3);
        jgrl = (RelativeLayout) findViewById(R.id.rl_gift4);
        iv = (ImageView) findViewById(R.id.down_first);
        iv2 = (ImageView) findViewById(R.id.down_two);
        iv3 = (ImageView) findViewById(R.id.down_three);
        iv4 = (ImageView) findViewById(R.id.down_four);
        dxrl.setOnClickListener(this);
        gxrl.setOnClickListener(this);
        chrl.setOnClickListener(this);
        jgrl.setOnClickListener(this);
        dxTv = (TextView) findViewById(R.id.bnt1_selectgift);
        chTv = (TextView) findViewById(R.id.bnt2_selectgift);
        gxTv = (TextView) findViewById(R.id.bnt3_selectgift);
        jgTv = (TextView) findViewById(R.id.bnt4_selectgift);
        rv = (RecyclerView) findViewById(R.id.rv_selectgift);
        GridLayoutManager manager = new GridLayoutManager(this,2);
        rv.setLayoutManager(manager);
        imageView = (ImageView) findViewById(R.id.selectgift_iv_goback);
    }
    @Override
    protected void initDate() {
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        /**
         * 请求全部的数据
         */
        selectGiftRvAdapter = new SelectGiftRvAdapter(this);
         netTool = new NetTool();
         netTool.getNetData(URLValues.SELECTGIFTALL, SelectGiftAllBean.class, new NetTool.NetListener<SelectGiftAllBean>() {
             @Override
             public void onSuccess(SelectGiftAllBean selectGiftAllBean) {
                 Toast.makeText(SelectGiftActivity.this, "解析成功", Toast.LENGTH_SHORT).show();
                 selectGiftRvAdapter.setSelectGiftAllBean(selectGiftAllBean);
                 rv.setAdapter(selectGiftRvAdapter);
             }
             @Override
             public void onError(String errorMsg) {
                 Toast.makeText(SelectGiftActivity.this, "解析失败", Toast.LENGTH_SHORT).show();
             }
         });
    }
    /**
     * 创建一个PopupWindow
     * tag 用来区分不同的pop里显示不同的容
     */
    public void initmPopupWindowView (int tag){
               final int a = tag;
        /**
         * 自定义一个view
         */
        View customView = getLayoutInflater().inflate(R.layout.popview_item , null );
        final RecyclerView rv = (RecyclerView) customView.findViewById(R.id.popview_rv);
        GridLayoutManager manager = new GridLayoutManager(this,3);
        rv.setLayoutManager(manager);
        //接口回调
        popRvAdapter.setSelectGiftOnClickListenner(this);
        /**
         * 请求网络数据
         */
        final NetTool netTool = new NetTool();
        netTool.getNetData(URLValues.SELECTEDGIFTAZ, FilterBean.class, new NetTool.NetListener<FilterBean>() {
            @Override
            public void onSuccess(FilterBean filterBean) {
                popRvAdapter.setFilterBean(filterBean);
                popRvAdapter.setTag(a);
                rv.setAdapter(popRvAdapter);
            }
            @Override
            public void onError(String errorMsg) {
            }
        });
        popupWindow = new PopupWindow(customView , ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        customView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (popupWindow != null && popupWindow.isShowing()){
                    popupWindow.dismiss();
                    popupWindow = null ;
                }
                return false;
            }
        });
    }
    /**
     * Button的监听事件
     * @param v
     * 传一个tag到新建的pop里
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.rl_gift:
                if (popupWindow != null&&popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    /**
                     * popupwindow收回时,更改button的背景图片
                     */
                    iv.setImageResource(R.mipmap.down);
                    return;
                } else {
                    /**
                     * 创建一个pop
                     */
                    initmPopupWindowView(0);
                    popupWindow.showAsDropDown(v, 0, 5);
                    /**
                     * popupwindow展开时,更改button的背景图片
                     */
                    iv.setImageResource(R.mipmap.up);
                }
                break;
            case  R.id.rl_gift2:
                if (popupWindow != null&&popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    iv2.setImageResource(R.mipmap.down);
                    return;
                } else {
                    initmPopupWindowView(1);
                    popupWindow.showAsDropDown(v, 0, 5);
                    iv2.setImageResource(R.mipmap.up);

                }
                break;
            case  R.id.rl_gift3:
                if (popupWindow != null&&popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    iv3.setImageResource(R.mipmap.down);
                    return;
                } else {
                    initmPopupWindowView(2);
                    popupWindow.showAsDropDown(v, 0, 5);
                    iv3.setImageResource(R.mipmap.up);
                }
                break;
            case  R.id.rl_gift4:

                if (popupWindow != null&&popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    iv4.setImageResource(R.mipmap.down);
                    return;
                } else {
                    initmPopupWindowView(3);
                    popupWindow.showAsDropDown(v, 0, 5);
                    iv4.setImageResource(R.mipmap.up);
                }

                break;
            case R.id.selectgift_paixu_iv:
                if (pxPopupWindow != null && pxPopupWindow.isShowing()){
                    pxPopupWindow.dismiss();
                }else {
                    initPxPopupWindow();
                    pxPopupWindow.showAsDropDown(v, 0 , 5);
                }
                break;
            /**
             * 排序的点击事件
             * 给适配器set一个int
             * 来区分不同的排序方法
             */
            case R.id.px_tv1 :
                selectGiftRvAdapter.setA(0);
                pxPopupWindow.dismiss();
                break;
            case R.id.px_tv2 :
                selectGiftRvAdapter.setA(1);
                pxPopupWindow.dismiss();
                break;
            case R.id.px_tv3 :
                selectGiftRvAdapter.setA(2);
                pxPopupWindow.dismiss();
                break;
            case R.id.px_tv4 :
                selectGiftRvAdapter.setA(3);
                pxPopupWindow.dismiss();
                break;
            default:
                break;
        }
    }

    /**
     * 排序的popwindow
     */
    private void initPxPopupWindow() {
        View pxView = getLayoutInflater().inflate(R.layout.pxpopuwindow_item,null);
        pxPopupWindow = new PopupWindow(pxView ,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        TextView tvOne = (TextView) pxView.findViewById(R.id.px_tv1);
        TextView tvTwo = (TextView) pxView.findViewById(R.id.px_tv2);
        TextView tvThree = (TextView) pxView.findViewById(R.id.px_tv3);
        TextView tvFour = (TextView) pxView.findViewById(R.id.px_tv4);
        tvOne.setOnClickListener(this);
        tvTwo.setOnClickListener(this);
        tvThree.setOnClickListener(this);
        tvFour.setOnClickListener(this);
        pxView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (pxPopupWindow!= null && pxPopupWindow.isShowing()){
                    pxPopupWindow.dismiss();
                    pxPopupWindow = null ;
                }
                return false;
            }
        });
    }

    /**
     * 接口回调
     * @param position
     * @param filterBean
     * @param tag
     */
    @Override
    public void onClick(int position, FilterBean filterBean , int tag) {

        switch (tag) {
            case 0:
                if (position == 0) {
                    dx ="";
                    dxTv.setText("全部");

                } else {
                    dx ="&"+filterBean.getData().getFilters().get(tag).getKey()+"="+filterBean.getData().getFilters().get(tag).getChannels().get(position - 1).getKey();
                    Log.d("SelectGiftActivity", dx);
                    dxTv.setText(filterBean.getData().getFilters().get(tag).getChannels().get(position-1).getName());
                }

                popupWindow.dismiss();
                iv.setImageResource(R.mipmap.down);
                break;
            case 1:
                if (position == 0) {
                    ch ="";
                    chTv.setText("全部");
                } else {
                    ch ="&"+filterBean.getData().getFilters().get(tag).getKey()+"="+filterBean.getData().getFilters().get(tag).getChannels().get(position - 1).getKey();
                    Log.d("SelectGiftActivity", ch);
                    chTv.setText(filterBean.getData().getFilters().get(tag).getChannels().get(position-1).getName());
                 }
                popupWindow.dismiss();
                iv2.setImageResource(R.mipmap.down);
                break;
            case 2:
                if (position == 0) {
                    gx ="";
                    gxTv.setText("全部");
                } else {
                    gx = "&"+filterBean.getData().getFilters().get(tag).getKey()+"="+filterBean.getData().getFilters().get(tag).getChannels().get(position - 1).getKey();
                    Log.d("SelectGiftActivity", gx);
                    gxTv.setText(filterBean.getData().getFilters().get(tag).getChannels().get(position-1).getName());
                }
                popupWindow.dismiss();
                iv3.setImageResource(R.mipmap.down);

                break;
            case 3:
                if (position == 0) {
                    jg ="";
                    jgTv.setText("全部");
                } else {
                    jg ="&"+filterBean.getData().getFilters().get(tag).getKey()+"="+filterBean.getData().getFilters().get(tag).getChannels().get(position - 1).getKey();
                    Log.d("SelectGiftActivity", jg);
                    jgTv.setText(filterBean.getData().getFilters().get(tag).getChannels().get(position-1).getName());
                }
                popupWindow.dismiss();
                iv4.setImageResource(R.mipmap.down);
                break;

             }
          Log.d("SelectGiftActivity", dx+gx+ch+jg);
            NetTool netTool = new NetTool();
            netTool.getNetData(URLValues.SELECTGIFTALL+dx+ch+gx+jg, SelectGiftAllBean.class, new NetTool.NetListener<SelectGiftAllBean>() {
                @Override
                public void onSuccess(SelectGiftAllBean selectGiftAllBean) {
                    selectGiftRvAdapter.setSelectGiftAllBean(selectGiftAllBean);
                    rv.setAdapter(selectGiftRvAdapter);
                }
                @Override
                public void onError(String errorMsg) {
                }
            });


    }

}
